import Crawler from 'crawler'
const c = new Crawler({
    maxConnections: 2000,
    rateLimit: 1000,
    callback: (error,res,done)=> {
        if(error){
            console.log(error)
            return 
        }
        const $ = res.$
        console.log($('title').text())
    }
})
/**
 * Crawler上层封装
 */
class CrawlerPlus {


    public static async getFictionData(url: string){ //: Promise<cheerio.Root>
        return new Promise<string | Buffer>((resolve,reject)=>{  
            c.queue([{
                url,
                jquery: false,
                callback: (error,res,done) => {
                    if(error){
                        reject(error)
                    }
                    resolve(res.body)
                    done()
                }
            }])
        }).catch(error=>{
            console.log(error)
        })
    }

    public static ajax(url: string){
        return new Promise<string | Buffer>((resolve,reject)=>{  
            c.queue({
                uri: url,
                method: 'GET',
                //http2: true, //set http2 to be true will make a http2 request
                callback: (error, res, done) => {
                    if(error) {
                        console.error(error);
                        reject(error)
                    }
             
                    console.log(`inside callback`);
                    console.log(res.body);
                    resolve(res.body)
                    done()
                }
            })
        }).catch(error=>{
            console.log(error)
        })
        
    }
}

export default CrawlerPlus